package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class DynamicLayout extends Activity {
	final static int ROOT_LAYOUT_ID =	0;
	
	private LinearLayout root =	null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout layout =	new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams params =	new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		
		Button button =	new Button(this);
		button.setText("inflate");
		button.setLayoutParams(params);
		layout.addView(button);
		
		root =	layout;

		setContentView(layout);
		
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inflate();
			}
		});
	}
	
	public void inflate() {
		LinearLayout newLayout =	new LinearLayout(this);
		newLayout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams newParam =	new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		
		root.addView(newLayout);
		
		LayoutInflater inflater =	LayoutInflater.from(this);
		inflater.inflate(R.layout.activity_linear_layout, newLayout);
	}
}
